package mall.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import mall.pojo.Category;
import mall.pojo.Orderdetail;
import mall.pojo.Orders;

@Repository
public class OrderDetailDAOImpl implements OrderDetailDAO {

	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public void createOrderDetail(Orderdetail orderdetail) {
		sessionFactory.getCurrentSession().save(orderdetail);
	}

	@Override
	public int deleteOrderDetail(int orderId, int productId) {
		return 0;
	}

	@Override
	public int updateOrderDetail(Orderdetail orderdetail) {
		String hql="UPDATE Orderdetail SET price= :price , quantity = :quantity WHERE order_id = :order_id AND product_id = :product_id";
		Query query= sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter("price", orderdetail.getPrice());
		query.setParameter("quantity", orderdetail.getQuantity());
		query.setParameter("order_id", orderdetail.getOrders().getId());
		query.setParameter("product_id", orderdetail.getProduct().getId());
		return query.executeUpdate();
	}

	@Override
	public List<Orderdetail> getOrderDetailShop(int orderId) {
		String hql="FROM Orderdetail OD WHERE OD.orders.id= "+orderId;
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return (List<Orderdetail>)query.list();
	}

}
